package com.example.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.system.domain.vo.SysUserOpenVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.example.base.system.domain.SysUserOpen;
import com.example.base.system.mapper.SysUserOpenMapper;
import com.example.base.system.service.SysUserOpenService;
import org.springframework.cache.annotation.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;


import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2024-04-11
 */
@Service
@Transactional
@CacheConfig(cacheNames = "SysUserOpenServiceImpl")
public class SysUserOpenServiceImpl extends ServiceImpl<SysUserOpenMapper, SysUserOpen> implements SysUserOpenService {

    @Cacheable(cacheNames = "sysuseropen",key = "#id")
    @Override
    public SysUserOpen getById(Serializable id){
        return super.getById(id);
    }

    @CacheEvict(cacheNames = "sysuseropen", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(cacheNames = "sysuseropen", key = "#sysuseropen.id")
    @Override
    public boolean updateById(SysUserOpen sysuseropen) {
        return super.updateById(sysuseropen);
    }

    @CachePut(cacheNames = "sysuseropen", key = "#sysuseropen.id")
    public SysUserOpen saveSysUserOpen(SysUserOpen sysuseropen) throws GlobalExcetion {
        super.save(sysuseropen);
        return sysuseropen;
    }

    @CacheEvict(cacheNames = "sysuseropen", key = "#sysuseropen.id")
    public SysUserOpen updateSysUserOpenById(SysUserOpen sysuseropen) throws GlobalExcetion{
        super.updateById(sysuseropen);
        return sysuseropen;
    }


    public SysUserOpen saveOrUpdateSysUserOpen(SysUserOpenVo sysuseropenVo) throws GlobalExcetion{
        SysUserOpen sysuseropen=new SysUserOpen();
        BeanUtils.copyProperties(sysuseropenVo,sysuseropen);
        if(sysuseropenVo.getUuid()!=null){
            SysUserOpen sysuseropenEntity=super.baseMapper.selectById(sysuseropenVo.getUuid());
            if(sysuseropenEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            super.baseMapper.updateById(sysuseropen);
        }else{
            super.baseMapper.insert(sysuseropen);
        }
        return sysuseropen;
    }

    public IPage<SysUserOpen> page(Integer page, Integer pageSize) throws GlobalExcetion{
        LambdaQueryWrapper<SysUserOpen> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<SysUserOpen> iPage=super.baseMapper.selectPage(new Page<>(page,pageSize),lambdaQueryWrapper);
        return iPage;
    }

    @Override
    public SysUserOpen getSysUserOpenByUserName(String userName) {
        LambdaQueryWrapper<SysUserOpen> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserOpen::getUsername,userName);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public SysUserOpen getSysUserOpenByMail(String mail) {
        LambdaQueryWrapper<SysUserOpen> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserOpen::getEMail,mail);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public SysUserOpen getSysUserOpenByPhone(String phone) {
        LambdaQueryWrapper<SysUserOpen> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserOpen::getMobile,phone);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }
}
